# Archives
function extract {
  if [ -z "$1" ]; then
    echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
  else
    if [ -f $1 ]; then
      case $1 in
        *.tar.bz2)   tar xvjf $1    ;;
        *.tar.gz)    tar xvzf $1    ;;
        *.tar.xz)    tar xvJf $1    ;;
        *.lzma)      unlzma $1      ;;
        *.bz2)       bunzip2 $1     ;;
        *.rar)       unrar x -ad $1 ;;
        *.gz)        gunzip $1      ;;
        *.tar)       tar xvf $1     ;;
        *.tbz2)      tar xvjf $1    ;;
        *.tgz)       tar xvzf $1    ;;
        *.zip)       unzip $1       ;;
        *.Z)         uncompress $1  ;;
        *.7z)        7z x $1        ;;
        *.xz)        unxz $1        ;;
        *.exe)       cabextract $1  ;;
        *)           echo "extract: '$1' - unknown archive method" ;;
      esac
    else
      echo "$1 - file does not exist"
    fi
  fi
}

#transform file code to UTF_8
function text_utf8 {
	if [ -e $1 ]; then
		iconv -f GB2312 -t UTF-8 $1 -o $1
	else
		echo "transform ERROR"
	fi
}

# Apply dark scheme for correspondending applications
function dark_mode {
	sed -i '/\/light-256/s/light/dark/' ~/.taskrc
	sed -i '/lightTheme/s/true/false/' ~/.config/jesseduffield/lazygit/config.yml
	sed -i 's/Adwaita-Dark/Adwaita/' ~/.config/qt5ct/qt5ct.conf
	sed -i 's/Adwaita/Adwaita-Dark/' ~/.config/qt5ct/qt5ct.conf
	sed -i '/background/s/light/dark/' ~/.config/nvim/init.vim
	sed -i '/useDarkTheme/s/false/true/' ~/.config/qv2ray/Qv2ray.conf
	sed -i '/CreatorTheme/s/light/dark/' ~/.config/QtProject/QtCreator.ini
	gsettings set org.gnome.desktop.interface gtk-theme Adwaita-dark
	gsettings set org.gnome.shell.extensions.user-theme name Adwaita-dark-shell
}

# Apply light scheme for correspondending applications
function light_mode {
	sed -i '/\/dark-256/s/dark/light/' ~/.taskrc
	sed -i '/lightTheme/s/false/true/' ~/.config/jesseduffield/lazygit/config.yml
	sed -i 's/Adwaita-Dark/Adwaita/' ~/.config/qt5ct/qt5ct.conf
	sed -i '/background/s/dark/light/' ~/.config/nvim/init.vim
	sed -i '/useDarkTheme/s/true/false/' ~/.config/qv2ray/Qv2ray.conf
	sed -i '/Theme/s/dark/light/' ~/.config/QtProject/QtCreator.ini
	gsettings set org.gnome.desktop.interface gtk-theme Adwaita
	gsettings set org.gnome.shell.extensions.user-theme name Adwaita-light-shell
}

# Acquire the root directory of the current script
function curt_root()
{
    echo $(cd $(dirname `[ -h $0 ] && readlink $0 || echo $0`); pwd)
}

# Travel dir
function travelDir {
	if [ -d $1 ]
	then
		for file in `ls $1`
		do
			if [ -d $1"/"$file ]
			then
				travelDir $1"/"$file
			else
				echo "$1/$file"
			fi
		done
	else
		echo "ERROR!your destination document of $1 is not a Directory"
	fi
	
}

# get sudo authorization
function get_sudo {
	if [ $# -eq 0 ]
	then
		echo "usage: get_sudo \$PASSWORD"
		exit
	else
		echo $1 | sudo -S su -c "exit" > /dev/null
	fi
}

# Create hard link for files in the given directory
function directory_hardlink {
	if [ $# -ne 2 ]
	then
		echo "error!usage:$0 $SRC_DIR $DST_DIR"
	else
		if [ -d $1 ] && [ -d $2 ]
		then
			for file in `ls $1`
			do
				if [ -d $1"/"$file ]
				then
					directory_hardlink $1"/"$file $2"/"$1
				else
					mkdir -p $2"/"$(basename $1)
					ln $file $2"/"$1"/"$file
				fi
			done
		else
			echo "ERROR!your destination documents of $1 or $2 is not a Directory"
		fi
	fi
}

# Judge the giver repo is clear or not
function clear_Repo {
	if [ -d $1 ]
	then
		cd $1
		str=$(git status -s)
		if [ -z $str ]
		then
			return 0
		else
			return 1
		fi
	else
		echo "wrong parameter"
	fi
}

# xinput list command to check keyboard device
# xinput --list-props device ID to list device attribution
# Disable laptop keyboard
